﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace BaMusaSoft.RPS.RPSServices
{
    public class ClaimPrinter
    {
        readonly PrintableClaim claim;
        readonly ClaimMargins margins;
        public ClaimPrinter(PrintableClaim claim, ClaimMargins margins)
        {
            if (claim == null) throw new ArgumentNullException("claim");
            if (margins == null) throw new ArgumentNullException("margins");
            this.claim = claim;
            this.margins = margins;
        }
        public void Print()
        {
            PrintDocument p = new PrintDocument();
            
            p.PrintPage += PrintClaim;
            p.Print();
        }

        private void PrintClaim(object sender, PrintPageEventArgs e)
        {
            using (Graphics g = e.Graphics)
            {
                using (StringFormat format = new StringFormat())
                {
                    format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                    Brush brush = Brushes.Black;

                    Font font = new Font("Simplified Arabic", 14, FontStyle.Bold | FontStyle.Italic);
                    RectangleF printingArea = new RectangleF(margins.OutboxNoMargins.X , margins.OutboxNoMargins.Y ,
                        e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.OutboxNo, font, brush, printingArea, format);

                    printingArea = new RectangleF(margins.OutboxDateMargins.X, margins.OutboxDateMargins.Y,
                        e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.OutboxDate, font, brush, printingArea, format);

                    font = new Font("Simplified Arabic", 18, FontStyle.Bold | FontStyle.Italic);
                    brush = Brushes.Red;
                    printingArea = new RectangleF(margins.ClaimNoMargins.X, margins.ClaimNoMargins.Y,
                       e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.ClaimNo, font, brush, printingArea, format);

                    font = new Font("Simplified Arabic", 18, FontStyle.Bold | FontStyle.Italic);
                    brush = Brushes.Black;
                    printingArea = new RectangleF(margins.CustomerNameMargins.X, margins.CustomerNameMargins.Y,
                       e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.CustomerName, font, brush, printingArea, format);

                    printingArea = new RectangleF(margins.Note1Margins.X, margins.Note1Margins.Y,
                       e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.Note1, font, brush, printingArea, format);

                    printingArea = new RectangleF(margins.Note2Margins.X, margins.Note2Margins.Y,
                       e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.Note2, font, brush, printingArea, format);

                    printingArea = new RectangleF(margins.TotalMargins.X, margins.TotalMargins.Y,
                       e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.ClaimTotal.ToString(), font, brush, printingArea, format);

                    printingArea = new RectangleF(margins.TotalInWordsMargins.X, margins.TotalInWordsMargins.Y,
                       e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                    g.DrawString(claim.ClaimTotalInWords, font, brush, printingArea, format);

                    bool isFirstRow = true;
                    RectangleF rowMargins = new RectangleF();
                    font = new Font("Simplified Arabic", 10, FontStyle.Bold | FontStyle.Italic);
                    foreach (var detail in claim.Details)
                    {

                        if (isFirstRow)
                        {
                            rowMargins = new RectangleF(margins.DetailRentX, margins.DetailRowY, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
                            PrintRow(rowMargins, detail, g, format, font, brush);
                            isFirstRow = false;
                        }
                        else
                        {
                            rowMargins.Y = rowMargins.Y + margins.DetailsRowDistance;
                            PrintRow(rowMargins, detail, g, format, font, brush);
                        }
                       
                    }
                    font.Dispose();
                    brush.Dispose();


                }
                e.HasMorePages = false;
            }
        }
        private void PrintRow(RectangleF rowMargins, PrintableClaimDetails detail, Graphics g, StringFormat format, Font font, Brush brush)
        {
            g.DrawString(detail.Rent.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailRentPaidX;
            g.DrawString(detail.PaidRent.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailPropertyX;
            g.DrawString(detail.Property, font, brush, rowMargins, format);

            rowMargins.X = margins.DetailMaintX;
            g.DrawString(detail.Maintenance.ToString(), font, brush, rowMargins, format);

            rowMargins.X =  margins.DetailDepositX;
            g.DrawString(detail.Deposit.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailOtherX;
            g.DrawString(detail.Other.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailRentBalanceX;
            g.DrawString(detail.RentBalance.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailArrearRentX;
            g.DrawString(detail.ArrearRent.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailArrearMaintX;
            g.DrawString(detail.ArrearMaint.ToString(), font, brush, rowMargins, format);

            rowMargins.X = margins.DetailTotalX;
            g.DrawString(detail.Total.ToString(), font, brush, rowMargins, format);
        }
        
        private Font ScaleFont(Graphics g, Font font)
        {
            return new Font(
                font.FontFamily, font.SizeInPoints / 72f * g.DpiY, font.Style, GraphicsUnit.Pixel, font.GdiCharSet, font.GdiVerticalFont);
        }
    }
}
